const util = require('../../utils/util.js')
let globalData = getApp().globalData
Page({
  data: {
    id:0,
    product:null,
    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    scrollH: 0, //滚动总高度
    opcity: 0,
    iconOpcity: 0.5,
    bannerIndex:0,

    menuShow: false,
    popupShow: false,
    value: 1,
    collected: false
  },
  onLoad: function (options) {

console.log(options)
    util.request("/app/detail2.html", {id:options.id}, "GET", false,  false ).then((res) => {
      console.log(res)
      this.setData({
        id:options.id,
        product: res.data,
      })
    })
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      top: obj.top + (obj.height - 32) / 2
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            scrollH: res.windowWidth
          })
        }
      })
    });
  },
  bannerChange: function (e) {
    this.setData({
      bannerIndex: e.detail.current
    })
  },
  //页面滚动执行方式
  onPageScroll(e) {
    let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
    let opcity = scroll / this.data.scrollH;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  },
  back: function () {
    wx.navigateBack()
  },
  openMenu: function () {
    this.setData({
      menuShow: true
    })
  },
  closeMenu: function () {
    this.setData({
      menuShow: false
    })
  },
  showPopup: function () {
    var userid= globalData.userInfo.id;
    let postData = { id:this.data.id,num:1,userid:userid};
    console.log(postData)
    util.request("/app/addShopcar.html", postData, "GET", false,  false ).then((res) => {
      console.log(res)
      if (res.msg=="1") {
        util.toast("添加成功",2000, true);
        
      }else{
        util.toast('添加失败');
      }
    
      
     
    }).catch((res) => {
      console.log(res)
      util.toast('请重试');
    })
    
  },
  hidePopup: function () {
    this.setData({
      popupShow: false
    })
  },
  change: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  collecting: function () {
    this.setData({
      collected: !this.data.collected
    })
  },
  common: function () {
    util.toast("功能开发中~")
  },
  submit() {
    var userid= globalData.userInfo.id;
    let postData = { id:this.data.id,num:1,userid:userid};
    console.log(postData)
    util.request("/app/addShopcar.html", postData, "GET", false,  false ).then((res) => {
      console.log(res)
      if (res.msg=="1") {
        setTimeout(()=>{
          wx.reLaunch({
            url: '/pages/shopcart/shopcart'
          })
        },200);
      }else{
        util.toast('添加失败');
      }
    
      
     
    }).catch((res) => {
      console.log(res)
      util.toast('请重试');
    })
  },
  coupon(){
    wx.navigateTo({
      url: '../mall-extend/coupon/coupon'
    })
  }
})